﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* 简单的Rect类
*/

define(function (require, exports, module) {
    var FXREADER = require('../FXReader.js');
    /**
     * Tools for Rectangle
     * @constructor 
     */
    FXREADER.RectUtils = {
    	/**
    	  * @description Clone a rectangle object.
    	  * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
    	  * @return {FXREADER.CPDF_Rect} the cloned rectangle object.
    	*/
        Clone: function (srcRect) {
            return new FXREADER.CPDF_Rect(srcRect.left, srcRect.top, srcRect.right, srcRect.bottom);
        },
        /**
         * @description Copy value from a rectangle to another rectangle.
 		* @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
 		* @param {FXREADER.CPDF_Rect} destRect destination rectangle.
         */
        Copy: function (srcRect, destRect) {
            destRect.left = srcRect.left;
            destRect.top = srcRect.top;
            destRect.right = srcRect.right;
            destRect.bottom = srcRect.bottom;
        },
        Equal: function(srcRect,destRect)
        {
        	return (destRect.left === srcRect.left &&
            destRect.top === srcRect.top &&
            destRect.right === srcRect.right &&
            destRect.bottom === srcRect.bottom);
        },
        /**
         * @description Get width of a rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         * @return {Number} width of the rectangle..
         */
        Width: function (srcRect) {
            return Math.abs(srcRect.right - srcRect.left);
        },
        /**
         * @description Get height of a rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         * @return {Number} height of the rectangle.
         */
        Height: function (srcRect) {
            return Math.abs(srcRect.bottom - srcRect.top);
        },
        /**
         * @description set a rectangle to empty.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         */
        Empty: function (srcRect) {
            srcRect.left = srcRect.top = srcRect.right = srcRect.bottom = 0;
        },
        /**
         * @description Check whether a rectangle is empty.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         * @return {Boolean} return true if the rectangle is empty, otherwise return false.
         */
        IsEmpty: function (srcRect) {
            return srcRect.left >= srcRect.right || srcRect.bottom >= srcRect.top;
        },
        /**
         * @description Offset a rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         * @param {Number} x x-axis value to offset.
 		 * @param {Number} y y-axis value to offset.
         */
        Offset: function (srcRect, x, y) {
            srcRect.left += x;
            srcRect.right += x;
            srcRect.top += y;
            srcRect.bottom += y;
        },
        /**
         * @description Check whether a point is in a rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
 		 * @example 
 			var bPtInRect = FXREADER.RectUtils.PtInRect(rect,point);
 			var bPtInRect = FXREADER.RectUtils.PtInRect(rect,x,y);
         * @return {Boolean} return true if the point is in the rectangle, otherwise return false.
         */
        PtInRect: function (srcRect) {
            var x = y = 0;
            if (arguments.length == 2) {
                x = arguments[1].x;
                y = arguments[1].y;
            } else if (arguments.length == 3 && !isNaN(arguments[1]) && !isNaN(arguments[2])) {
                x = arguments[1];
                y = arguments[2];
            }
            return (x >= srcRect.left && x <= srcRect.right && y <= srcRect.top && y >= srcRect.bottom);
        },
        /**
         * @description Intersect two rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
 		 * @param {FXREADER.CPDF_Rect} otherRect another rectangle.
         */
        Intersect: function (srcRect, otherRect) {
            srcRect.left = Math.max(srcRect.left, otherRect.left);
            srcRect.top = Math.min(srcRect.top, otherRect.top);
            srcRect.right = Math.min(srcRect.right, otherRect.right);
            srcRect.bottom = Math.max(srcRect.bottom, otherRect.bottom);
          //  return (new FXREADER.CPDF_Rect(left, top, right, bottom));
        },
        /**
         * @description Normalize a rectangle to make sure rect.left<=rect.right and rect.bottom<=rect.top.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
         */
        Normalize: function (srcRect) {
            if (srcRect.left > srcRect.right) {
                var tmp = srcRect.left;
                srcRect.left = srcRect.right;
                srcRect.right = tmp;
            }
            if (srcRect.bottom > srcRect.top) {
                var tmp = srcRect.bottom;
                srcRect.bottom = srcRect.top;
                srcRect.top = tmp;
            }
        },
        /**
         * @description Union two rectangle.
 		 * @param {FXREADER.CPDF_Rect} srcRect  source rectangle.
 		 * @param {FXREADER.CPDF_Rect} otherRect another rectangle.
         */
        Union: function (srcRect, otherRect) {
            var union_left = Math.min(srcRect.left, otherRect.left),
                union_top = Math.max(srcRect.top, otherRect.top),
                union_right = Math.max(srcRect.right, otherRect.right),
                union_bottom = Math.min(srcRect.bottom, otherRect.bottom);
            srcRect.left = union_left;
            srcRect.top = union_top;
            srcRect.right = union_right;
            srcRect.bottom = union_bottom;
        },
        SetRect:function(srcRect)
        {
            if (arguments.length == 2) {
                FXREADER.RectUtils.Copy(arguments[1], this);
            } else if (arguments.length == 5) {
                if (!isNaN(arguments[1]) && !isNaN(arguments[2]) && !isNaN(arguments[3]) && !isNaN(arguments[4])) {
                    srcRect.left = arguments[1];
                    srcRect.top = arguments[2];
                    srcRect.right = arguments[3];
                    srcRect.bottom = arguments[4];
                }
            }
        }
    };
    /**
     * Rectange class.
     * @class Rectange class.
     * @constructor use FXREADER.CPDF_Rect(left,top,right,bottom) or FXREADER.CPDF_Rect(anotherRect);
     * @return {FXREADER.CPDF_Rect} a rectange object.
     */
    FXREADER.CPDF_Rect = function () {
        this.left = this.top = this.right = this.bottom = 0;


        switch (arguments.length) {
            case 1: FXREADER.RectUtils.SetRect(this, arguments[0]); break;
            case 4: FXREADER.RectUtils.SetRect(this, arguments[0], arguments[1], arguments[2], arguments[3]); break;
        }
    };

    return FXREADER.CPDF_Rect;
});
